package com.neuronbit.businessflow.intellij.dialog;


import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.datatransfer.*;
import java.io.IOException;

public class TreeNodeTransferable implements Transferable, ClipboardOwner {

    private FieldTreeListChooseDialog.FieldTreeNode data;
    public static DataFlavor nodeFlavor;

    static {
        try {
            nodeFlavor = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType +
                    ";class=\"" + DefaultMutableTreeNode.class.getName() + "\"");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public TreeNodeTransferable(FieldTreeListChooseDialog.FieldTreeNode data) {
        this.data = data;
    }

    public DataFlavor[] getTransferDataFlavors() {
        return new DataFlavor[]{nodeFlavor};
    }

    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return nodeFlavor.equals(flavor);
    }

    public Object getTransferData(DataFlavor flavor)
            throws UnsupportedFlavorException, IOException {
        if (!isDataFlavorSupported(flavor)) {
            throw new UnsupportedFlavorException(flavor);
        }
        return data;
    }

    public void lostOwnership(Clipboard clipboard, Transferable contents) {
    }
}
